package org.example.simpleboot;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author Administrator
 */
public class SpringApplication {

    private static final String HTTP_CONNECTOR_PROTOCOL_1_1 = "HTTP/1.1";

    private static final int HTTP_SERVER_DEFAULT_PORT = 8080;

    public static void run() {

        try {
            Path tempDocDir = Files.createTempDirectory("tomcat-temp-doc-dir");

            Tomcat server = new Tomcat();
            Connector connector = new Connector(HTTP_CONNECTOR_PROTOCOL_1_1);
            connector.setPort(HTTP_SERVER_DEFAULT_PORT);

            server.addWebapp("/", tempDocDir.toFile().getAbsolutePath());
            server.getService().addConnector(connector);
            server.start();
            server.getServer().await();
        } catch (LifecycleException | IOException | ServletException e) {
            e.printStackTrace();
        }
    }
}
